package com.whfc.openapi.api.fse.service.impl;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.enums.DevicePlatform;
import com.whfc.common.third.deye.*;
import com.whfc.common.util.MathUtil;
import com.whfc.fse.dto.*;
import com.whfc.fse.enums.FseLiftDoorStatus;
import com.whfc.fse.enums.FseWarnRuleType;
import com.whfc.openapi.api.fse.service.OpenApiFseDeyeService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qzexing
 * @version 1.0
 * @date 2022/3/2 17:16
 */
@Service
public class OpenApiFseDeyeServiceImpl implements OpenApiFseDeyeService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void loginDataCrane(String data) {
    }

    @Override
    public void baseDataCrane(String data) {
        DeyeCraneBaseData deyeCraneBaseData = JSON.parseObject(data, DeyeCraneBaseData.class);
        if (deyeCraneBaseData == null) {
            return;
        }
        FseCraneParamDTO fseCraneParamDTO = new FseCraneParamDTO();
        fseCraneParamDTO.setPlatform(DevicePlatform.deye.name());
        fseCraneParamDTO.setSn(deyeCraneBaseData.getHxzId());
        fseCraneParamDTO.setForeArmLength(MathUtil.float2Double(deyeCraneBaseData.getFrontArmLength()));
        fseCraneParamDTO.setRearArmLength(MathUtil.float2Double(deyeCraneBaseData.getBackArmLength()));
        fseCraneParamDTO.setTowerArmHeight(MathUtil.float2Double(deyeCraneBaseData.getArmHeight()));
        fseCraneParamDTO.setMaxWeight(MathUtil.float2Double(deyeCraneBaseData.getMaxWeight()));
        fseCraneParamDTO.setMaxWindSpeed(MathUtil.float2Double(deyeCraneBaseData.getRatedWindSpeed()));
        // 发送队列处理
        amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_PARAM_DATA, JSON.toJSONString(fseCraneParamDTO));
    }

    @Override
    public void realtimeDataCrane(String data) {
        DeyeCraneRealtimeData deyeCraneRealtimeData = JSON.parseObject(data, DeyeCraneRealtimeData.class);
        if (deyeCraneRealtimeData == null) {
            return;
        }
        FseCraneDataDTO fseCraneDataDTO = new FseCraneDataDTO();
        fseCraneDataDTO.setPlatform(DevicePlatform.deye.name());
        fseCraneDataDTO.setSn(deyeCraneRealtimeData.getHxzId());
        fseCraneDataDTO.setTime(deyeCraneRealtimeData.getRTime());
        fseCraneDataDTO.setHeight(MathUtil.float2Double(deyeCraneRealtimeData.getHeight()));
        fseCraneDataDTO.setWeight(MathUtil.float2Double(deyeCraneRealtimeData.getWeight()));
        fseCraneDataDTO.setRange(MathUtil.float2Double(deyeCraneRealtimeData.getRRange()));
        fseCraneDataDTO.setWindSpeed(MathUtil.float2Double(deyeCraneRealtimeData.getWindSpeed()));
        fseCraneDataDTO.setDipAngle(MathUtil.float2Double(deyeCraneRealtimeData.getObliguity()));
        fseCraneDataDTO.setMaxMomentRatio(MathUtil.float2Double(deyeCraneRealtimeData.getMoment()));
        //处理报警状态
        int craneStatus = parseDeyeCraneValue(deyeCraneRealtimeData);
        fseCraneDataDTO.setCraneStatus(craneStatus);
        // 发送队列处理
        amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_REAL_DATA, JSON.toJSONString(fseCraneDataDTO));
    }

    @Override
    public void workDataCrane(String data) {
        DeyeCraneWorkData deyeCraneWorkData = JSON.parseObject(data, DeyeCraneWorkData.class);
        if (deyeCraneWorkData == null) {
            return;
        }
        FseCraneRecordDTO craneRecordDTO = new FseCraneRecordDTO();
        craneRecordDTO.setPlatform(DevicePlatform.deye.name());
        craneRecordDTO.setSn(deyeCraneWorkData.getHxzId());
        craneRecordDTO.setStartTime(deyeCraneWorkData.getWorkStartTime());
        craneRecordDTO.setEndTime(deyeCraneWorkData.getWorkEndTime());
        craneRecordDTO.setStartHeight(MathUtil.float2Double(deyeCraneWorkData.getWorkStartHeight()));
        craneRecordDTO.setEndHeight(MathUtil.float2Double(deyeCraneWorkData.getWorkEndHeight()));
        craneRecordDTO.setStartRange(MathUtil.float2Double(deyeCraneWorkData.getWorkStartRange()));
        craneRecordDTO.setEndRange(MathUtil.float2Double(deyeCraneWorkData.getWorkEndRange()));
        craneRecordDTO.setMaxWeight(MathUtil.float2Double(deyeCraneWorkData.getWorkWeight()));
        craneRecordDTO.setStartTurnAngle(MathUtil.float2Double(deyeCraneWorkData.getWorkStartAngle()));
        craneRecordDTO.setEndTurnAngle(MathUtil.float2Double(deyeCraneWorkData.getWorkEndAngle()));
        // 推送数据
        amqpTemplate.convertAndSend(QueueConst.FSE_CRANE_RECORD_DATA, JSON.toJSONString(craneRecordDTO));
    }

    @Override
    public void alarmDataCrane(String data) {
    }

    @Override
    public void alarmChangeDataCrane(String data) {
    }

    @Override
    public void curveDataCrane(String data) {
    }

    @Override
    public void loginDataElevator(String data) {
    }

    @Override
    public void baseDataElevator(String data) {
        DeyeLiftBaseData deyeLiftBaseData = JSON.parseObject(data, DeyeLiftBaseData.class);
        if (deyeLiftBaseData == null) {
            return;
        }
        FseLiftParamDTO fseLiftParamDTO = new FseLiftParamDTO();
        fseLiftParamDTO.setPlatform(DevicePlatform.deye.name());
        fseLiftParamDTO.setSn(deyeLiftBaseData.getHxzId());
        fseLiftParamDTO.setMaxHeight(MathUtil.float2Double(deyeLiftBaseData.getRatedHeight()));
        fseLiftParamDTO.setMaxWeight(MathUtil.float2Double(deyeLiftBaseData.getRatedWeight()));
        fseLiftParamDTO.setMaxPersonNo(deyeLiftBaseData.getRatedPeopleCnt());
        fseLiftParamDTO.setMaxDipAngle(MathUtil.float2Double(deyeLiftBaseData.getRatedObliguityX()));
        fseLiftParamDTO.setMaxSpeed(MathUtil.float2Double(deyeLiftBaseData.getRatedUpSpeed()));
        // 发送队列处理
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_PARAM_DATA, JSON.toJSONString(fseLiftParamDTO));
    }

    @Override
    public void realtimeDataElevator(String data) {
        DeyeLiftRealtimeData deyeLiftRealtimeData = JSON.parseObject(data, DeyeLiftRealtimeData.class);
        if (deyeLiftRealtimeData == null) {
            return;
        }
        FseLiftDataDTO fseLiftData = new FseLiftDataDTO();
        fseLiftData.setPlatform(DevicePlatform.deye.name());
        fseLiftData.setSn(deyeLiftRealtimeData.getHxzId());
        fseLiftData.setTime(deyeLiftRealtimeData.getRTime());
        fseLiftData.setFloor(deyeLiftRealtimeData.getFloor());
        fseLiftData.setHeight(MathUtil.float2Double(deyeLiftRealtimeData.getHeight()));
        fseLiftData.setWeight(MathUtil.float2Double(deyeLiftRealtimeData.getWeight()));
        fseLiftData.setDipAngle(MathUtil.float2Double(deyeLiftRealtimeData.getObliguityX()));
        fseLiftData.setPersonNo(deyeLiftRealtimeData.getPeopleCnt());
        fseLiftData.setSpeed(MathUtil.float2Double(deyeLiftRealtimeData.getSpeed()));
        fseLiftData.setWindSpeed(MathUtil.float2Double(deyeLiftRealtimeData.getWindSpeed()));
        int doorState = deyeLiftRealtimeData.getDoorState();
        Integer frontDoorState = FseLiftDoorStatus.CLOSE.getValue();
        if (doorState != 0) {
            frontDoorState = FseLiftDoorStatus.OPEN.getValue();
        }
        fseLiftData.setFrontDoorState(frontDoorState);
        fseLiftData.setBackDoorState(FseLiftDoorStatus.CLOSE.getValue());
        // 处理报警
        fseLiftData.setLiftStatus(parseDeyeLiftValue(deyeLiftRealtimeData));
        // 推送数据
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_REAL_DATA, JSON.toJSONString(fseLiftData));
    }


    @Override
    public void workDataElevator(String data) {
        DeyeLiftWorkData deyeLiftWorkData = JSON.parseObject(data, DeyeLiftWorkData.class);
        if (deyeLiftWorkData == null) {
            return;
        }
        FseLiftRecordDTO liftRecordDTO = new FseLiftRecordDTO();
        liftRecordDTO.setPlatform(DevicePlatform.deye.name());
        liftRecordDTO.setSn(deyeLiftWorkData.getHxzId());
        liftRecordDTO.setStartTime(deyeLiftWorkData.getWorkStartTime());
        liftRecordDTO.setEndTime(deyeLiftWorkData.getWorkEndTime());
        liftRecordDTO.setWeight(MathUtil.float2Double(deyeLiftWorkData.getMaxWeight()));
        liftRecordDTO.setPersonNo(deyeLiftWorkData.getPeopleCnt());
        liftRecordDTO.setStartHeight(MathUtil.float2Double(deyeLiftWorkData.getStartHeight()));
        liftRecordDTO.setEndHeight(MathUtil.float2Double(deyeLiftWorkData.getEndHeight()));
        liftRecordDTO.setDirection(0);
        liftRecordDTO.setAvgSpeed(0D);
        liftRecordDTO.setDipAngleX(MathUtil.float2Double(deyeLiftWorkData.getMaxObliguityX()));
        liftRecordDTO.setDipAngleY(MathUtil.float2Double(deyeLiftWorkData.getMaxObliguityY()));
        // 推送数据
        amqpTemplate.convertAndSend(QueueConst.FSE_LIFT_RECORD_DATA, JSON.toJSONString(liftRecordDTO));
    }

    @Override
    public void alarmDataElevator(String data) {
    }

    @Override
    public void upImage(String data) {
    }

    @Override
    public void operatorRecord(String data) {
    }

    @Override
    public void gpsData(String data) {
    }

    /**
     * 处理德业升降机报警
     *
     * @param deyeLiftRealtimeData 德业升降机实时数据
     * @return 风潮报警编码
     */
    private Integer parseDeyeLiftValue(DeyeLiftRealtimeData deyeLiftRealtimeData) {
        if (deyeLiftRealtimeData.getWeightAlarm() != null && deyeLiftRealtimeData.getWeightAlarm() == 1) {
            return FseWarnRuleType.LIFT_WEIGH.value();
        } else if (deyeLiftRealtimeData.getSpeedAlarm() != null && deyeLiftRealtimeData.getSpeedAlarm() == 1) {
            return FseWarnRuleType.LIFT_SPEED.value();
        }
        return 0;
    }

    /**
     * 处理德业塔机报警
     *
     * @param deyeCraneRealtimeData 德业塔机实时数据
     * @return 风潮报警编码
     */
    private Integer parseDeyeCraneValue(DeyeCraneRealtimeData deyeCraneRealtimeData) {
        if (deyeCraneRealtimeData.getMultiAlarmAll() != null && deyeCraneRealtimeData.getMultiAlarmAll() == 1) {
            return FseWarnRuleType.CRANE_ANTI_COLLISION.value();
        } else if (deyeCraneRealtimeData.getMomentAlarm() != null && deyeCraneRealtimeData.getMomentAlarm() == 1) {
            return FseWarnRuleType.CRANE_FORCE_MOMENT.value();
        } else if (deyeCraneRealtimeData.getMaxRangeAlarm() != null && deyeCraneRealtimeData.getMaxRangeAlarm() == 1) {
            return FseWarnRuleType.CRANE_RANGE.value();
        } else if (deyeCraneRealtimeData.getHeightAlarm() != null && deyeCraneRealtimeData.getHeightAlarm() == 1) {
            return FseWarnRuleType.CRANE_HEIGHT_RANGE.value();
        } else if (deyeCraneRealtimeData.getPosAngleAlarm() != null && deyeCraneRealtimeData.getPosAngleAlarm() == 1) {
            return FseWarnRuleType.CRANE_ROTARY_LIMIT.value();
        }  else if (deyeCraneRealtimeData.getWindSpeedAlarm() != null && deyeCraneRealtimeData.getWindSpeedAlarm() == 1) {
            return FseWarnRuleType.CRANE_WIND_SPEED.value();
        } else if (deyeCraneRealtimeData.getObliguityAlarm() != null && deyeCraneRealtimeData.getObliguityAlarm() == 1) {
            return FseWarnRuleType.CRANE_TURN.value();
        }
        return 0;
    }
}